﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using EnvDTE;
using EnvDTE80;

namespace ForgeIt.Commands
{
    ///<summary>
    /// Abstract class that represents a command implementation for IDTCommandTargets
    ///</summary>
    public abstract class BaseCommand: IDTCommandTarget
    {
        private readonly DTE2 _applicationObject;
        private readonly Connect _connectorClass;
        protected List<string> validExtensions = new List<string>();

        // ReSharper disable BitwiseOperatorOnEnumWihtoutFlags
        protected const vsCommandStatus StatusInvisible = vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusInvisible;
        protected const vsCommandStatus StatusDisabled = vsCommandStatus.vsCommandStatusSupported;
        protected const vsCommandStatus StatusEnabled = vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
        // ReSharper restore BitwiseOperatorOnEnumWihtoutFlags

        protected BaseCommand(DTE2 applicationObject, Connect connectorClass)
        {
            _applicationObject = applicationObject;
            _connectorClass = connectorClass;
        }

        protected DTE2 ApplicationObject
        {
            get { return _applicationObject; }
        }

        protected Connect ConnectorClass
        {
            get { return _connectorClass; }
        }

        ///<summary>
        ///</summary>
        ///<param name="CmdName"></param>
        ///<param name="ExecuteOption"></param>
        ///<param name="VariantIn"></param>
        ///<param name="VariantOut"></param>
        ///<param name="Handled"></param>
        ///<exception cref="NotImplementedException"></exception>
        public abstract void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VariantIn,
                                  ref object VariantOut, ref bool Handled);

        ///<summary>
        ///</summary>
        ///<param name="CmdName"></param>
        ///<param name="NeededText"></param>
        ///<param name="StatusOption"></param>
        ///<param name="CommandText"></param>
        public virtual void QueryStatus(string CmdName, vsCommandStatusTextWanted NeededText, ref vsCommandStatus StatusOption, ref object CommandText)
        {
            if (NeededText != vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) return;
            // ReSharper disable PossibleNullReferenceException
            StatusOption = ApplicationObject.SelectedItems.Cast<SelectedItem>().Where(ext => string.IsNullOrEmpty(Path.GetExtension(ext.Name)) ||
                                                                                 validExtensions.Contains(Path.GetExtension(ext.Name).ToLower())).Count() > 0
                                                                                 ? StatusEnabled : StatusInvisible;
            // ReSharper restore PossibleNullReferenceException

        }

        protected void Log(string message)
        {
            ConnectorClass.Log(message);
        }
        
        protected void Log(string message, bool popup)
        {
            ConnectorClass.Log(message, popup);
        }


        protected void Log(Exception error)
        {
            ConnectorClass.Log(error);
        }
    }
}
